/*
 * Dictionary.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.dictionary;

import de.studiorutton.access.FileAccessException;
import de.studiorutton.gerengdic.Prefs;
import de.studiorutton.gerengdic.databaseaccess.ContentAccess;
import de.studiorutton.gerengdic.databaseaccess.DatabaseAccessException;
import de.studiorutton.gerengdic.history.DictionaryCommand;
import de.studiorutton.gerengdic.history.History;
import de.studiorutton.gerengdic.history.HistoryProvider;
import de.studiorutton.gerengdic.library.LibraryDatabase;
import de.studiorutton.gerengdic.library.LibraryException;

public class Dictionary {
	private final static String TAG = "learningwords";
	public static int matchFullWordMode;
	
	private static Dictionary INSTANCE = null;

	private IDictionaryView dv;
	private DictionaryWorker dl;
	private DictionaryAdapter dAdapter = null;

	private String searchString = "";
	private String lastSearchString = "";
	private int crrLanguage = ContentAccess.LANGUAGE_BOTH;

	private int maxResults = 200;
	private int dictionaryPosition;

	private Dictionary() {
	}
	
	public static Dictionary get() {
		if ( INSTANCE == null )
			INSTANCE = new Dictionary();
		return INSTANCE;
	}
	
	public static void release() {
		INSTANCE.terminateSearchThread();
		INSTANCE = null;
	}

	public Dictionary init() throws FileAccessException,
			DatabaseAccessException {
		if (dl == null)
			dl = new DictionaryWorker( LibraryDatabase.get().getFileLocation( LibraryDatabase.DE_EN_DICT ));
		return this;
	}

	public void setDictionaryView(IDictionaryView dv) {
		this.dv = dv;
		if (dAdapter != null)
			dAdapter.setDictionaryView(dv);
	}

	public int getDictionaryPosition() {
		return dictionaryPosition;
	}

	public void setDictionaryPosition(int dictionaryPosition) {
		this.dictionaryPosition = dictionaryPosition;
	}

	public void setLanguage(int language) {
		this.crrLanguage = language;
	}

	public int getLanguage() {
		return crrLanguage;
	}

	public boolean isRunning() {
		if (dl == null)
			return false;
		return dl.getIsRunning();
	}

	public void setSearchString(String searchString) {
		String searchStringTrimmed = searchString.trim();
		lastSearchString = this.searchString;
		this.searchString = searchStringTrimmed;
	}

	public String getSearchString() {
		return searchString;
	}

	public boolean equalsToLastSearchString(String s) {
		return s.equals(lastSearchString);
	}

	public DictionaryAdapter getDictionaryAdapter() {
		if (dAdapter == null) {
			DictionaryRecordContent.setLanguage(crrLanguage);
			dAdapter = new DictionaryAdapter(dv, this);
		}

		return dAdapter;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void updateLanguage(DictionaryActivity activity)
			throws LibraryException {
		if (dAdapter == null)
			dAdapter = getDictionaryAdapter();
		else {
			DictionaryRecordContent.setLanguage(crrLanguage);
			dAdapter.getData().reinit(DictionaryRecord.dComparer);

		}
		if (searchString.length() >= 3)
			startDictionarySearch();

	}

	public void startDictionarySearch() throws LibraryException {
		DictionaryAdapter da = getDictionaryAdapter();
		if (searchString.length() < 3) {
			cancelDictionarySearch();
			da.clear();
			return;
		}
		RecordUpdateHandler ruh = new RecordUpdateHandler(dv, dAdapter, this);
		da.clear();
		boolean aMatchInFulltextSearch = Prefs.getInstance()
				.getAMatchInFulltextSearch();
		dl.searchMatchingRecords(searchString, crrLanguage, matchFullWordMode,
				aMatchInFulltextSearch, maxResults, ruh);
	}

	public void clear() {
		dAdapter.clear();
	}

	public void cancelDictionarySearch() {
		if (dl != null) {
			dl.cancelSearchOperation();
		}
	}

	public void terminateSearchThread() {
		if (dl != null)
			dl.stopSearchThread();
	}

	public void addHistoryEntry() {
		int searchWay = DictionaryCommand.SEARCH_WAY_FULLTEXT;
		String searchString = this.searchString;
		int searchLanguage = crrLanguage;

		HistoryProvider hp = History.get().getHistoryProvider();
		hp.deleteOld(searchWay, searchString, searchLanguage);
		hp.insert(searchWay, searchString, searchLanguage);
	}

}
